const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

function secret(v){
	return bcrypt.hashSync(v,10);
}

const MONGOOSE_HOST = 'mongodb://119.28.139.194:27017';
const PROJECT_NAME = 'express-auth';
mongoose.connect(`${MONGOOSE_HOST}/${PROJECT_NAME}`,{
	useNewUrlParser:true,
	useUnifiedTopology:true,
	useCreateIndex:true
}).then(() => {
	// console.log('p',p);
}).catch(err => {
	console.log('err',err);
});

const UserSchema = new mongoose.Schema({
	username:{
		type:String,
		unique:true
	},
	password:{
		type:String,
		set:secret,
		unique:true
	}
});

const User = mongoose.model('User',UserSchema,'User');
// User.db.dropCollection('User')

module.exports = { User };
